package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.databinding.ActivityPicManageBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.IntentUtil;
import stark.common.basic.utils.RxUtil;

public class PicManageActivity extends BaseAc<ActivityPicManageBinding> {

    private SelPictureAdapter pictureAdapter;
    private List<SelectMediaEntity> listDelete = new ArrayList<>();
    private boolean isEdit = false;
    private boolean isSelectAll = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_manage;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivPicManageBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvPicList.setLayoutManager(new GridLayoutManager(mContext, 3));
        pictureAdapter = new SelPictureAdapter();
        mDataBinding.rvPicList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(this);
        pictureAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (!isEdit) {
                    isEdit = true;
                    pictureAdapter.setEdit(isEdit);
                    pictureAdapter.notifyDataSetChanged();
                    mDataBinding.tvSelNum.setVisibility(View.VISIBLE);
                    mDataBinding.llPicManageBottom.setVisibility(View.VISIBLE);
                } else {
                    listDelete.clear();
                    isEdit = false;
                    pictureAdapter.setEdit(isEdit);
                    getData();
                    mDataBinding.tvSelNum.setText(getString(R.string.sel_num, listDelete.size()));
                    mDataBinding.tvSelNum.setVisibility(View.GONE);
                    mDataBinding.llPicManageBottom.setVisibility(View.GONE);
                }
                return true;
            }
        });
        mDataBinding.llSelectAll.setOnClickListener(this);
        mDataBinding.llDelete.setOnClickListener(this);
        mDataBinding.llShare.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llSelectAll://全选
                listDelete.clear();
                if (isSelectAll) {
                    for (SelectMediaEntity validDatum : pictureAdapter.getValidData()) {
                        validDatum.setChecked(false);
                    }
                } else {
                    for (SelectMediaEntity validDatum : pictureAdapter.getValidData()) {
                        validDatum.setChecked(true);
                        listDelete.add(validDatum);
                    }
                }
                isSelectAll();
                pictureAdapter.notifyDataSetChanged();
                break;
            case R.id.llDelete://删除
                if (listDelete.size() == 0) {
                    return;
                }
                delete();
                break;
            case R.id.llShare://分享
                share();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            if (pictureAdapter.getItem(position).isChecked()) {
                pictureAdapter.getItem(position).setChecked(false);
                listDelete.remove(pictureAdapter.getItem(position));
                pictureAdapter.notifyDataSetChanged();
            } else {
                listDelete.add(pictureAdapter.getItem(position));
                pictureAdapter.getItem(position).setChecked(true);
                pictureAdapter.notifyDataSetChanged();
            }
            isSelectAll();
        } else {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(pictureAdapter.getItem(position).getUri()), "image/*");
            startActivity(intent);
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvSelNum.setVisibility(View.GONE);
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 0));
        getData();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    pictureAdapter.setList(selectMediaEntities);
                    mDataBinding.rvPicList.setVisibility(View.VISIBLE);
                    mDataBinding.tvPicManageNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPicList.setVisibility(View.GONE);
                    mDataBinding.tvPicManageNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void isSelectAll() {
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, listDelete.size()));
        if (listDelete.size() == pictureAdapter.getValidData().size()) {
            mDataBinding.ivSelectAll.setImageResource(R.drawable.ayiquanx);
            isSelectAll = true;
        } else {
            mDataBinding.ivSelectAll.setImageResource(R.drawable.aquanx);
            isSelectAll = false;
        }
    }

    private void delete() {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        for (int i = 0; i < listDelete.size(); i++) {
                            //从此文件夹删除
                            mContext.getContentResolver().delete(Uri.parse(listDelete.get(i).getUri()), null, null);
                        }
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        listDelete.clear();
                        isEdit = false;
                        pictureAdapter.setEdit(isEdit);
                        getData();
                        dismissDialog();
                        ToastUtils.showShort(getString(R.string.delete_suc));
                        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, listDelete.size()));
                        mDataBinding.tvSelNum.setVisibility(View.GONE);
                        mDataBinding.llPicManageBottom.setVisibility(View.GONE);
                    }
                });
            }
        }, 1000);
    }

    private void share() {
        if (listDelete.size() == 0) {
            return;
        }
        List<String> picPathList = new ArrayList<>();
        for (SelectMediaEntity selectMediaEntity : listDelete) {
            picPathList.add(selectMediaEntity.getPath());
        }
        IntentUtil.shareImagesPath(mContext, picPathList);
    }


}